package com.zhuiyi.zhxy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhuiyi.zhxy.pojo.Admin;
import com.zhuiyi.zhxy.service.AdminService;
import com.zhuiyi.zhxy.util.MD5;
import com.zhuiyi.zhxy.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @Author: zhuiyi
 * @CreatTime: 2023/1/22 21:05
 **/
@Api(tags = "管理员管理器")
@RestController
@RequestMapping("/sms/adminController")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * Request URL: http://localhost:9001/sms/adminController/deleteAdmin
     * Request Method: DELETE
     */
    @ApiOperation("删除单个或多个管理员信息")
    @DeleteMapping("/deleteAdmin")
    public Result deleteAdmin(@ApiParam("要删除的管理员的id集合")@RequestBody List<Integer> ids){
        adminService.removeByIds(ids);
        return Result.ok();
    }

    /**
     * Request URL: http://localhost:9001/sms/adminController/saveOrUpdateAdmin
     * Request Method: POST
     */
    @ApiOperation("新增或修改管理员信息")
    @PostMapping("/saveOrUpdateAdmin")
    public Result saveOrUpdateAdmin(@ApiParam("JSON格式的管理员信息")@RequestBody Admin admin){
        if (admin.getId() == null || admin.getId() == 0) {
            admin.setPassword(MD5.encrypt(admin.getPassword()));
        }
        adminService.saveOrUpdate(admin);
        return Result.ok();
    }


    /**
     * Request URL: http://localhost:9001/sms/adminController/getAllAdmin/1/3?adminName=1
     * Request Method: GET
     */
    @GetMapping("/getAllAdmin/{pageNo}/{pageSize}")
    public Result getAllAdmin(
            @ApiParam("页码数") @PathVariable("pageNo") Integer pageNo,
            @ApiParam("页大小") @PathVariable("pageSize") Integer pageSize,
            @ApiParam("管理员姓名")  String adminName
    ){
        Page page =new Page(pageNo,pageSize);
        IPage<Admin> iPage = adminService.getAllAdmin(page,adminName);
        return Result.ok(iPage);
    }

}
